// pages/details/details.js
var {getGoodsDetail} = require("../../api/Home.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
      goodsList:[
          {
              goodsImgs:[
                  "/images/banner01.jpg",
                  "/images/banner02.jpg",
                  "/images/banner03.jpg",
                  "/images/banner04.jpg",
              ],
              id:5,
              goodsImage:"/images/lists01.webp",
              goodsName:"1商品名称",
              goodsAddress:"广州",
              goodsPrice:"200",
              goodsPriceOld:"2000",
              num:1,
          }],
          carnum:0,
          liked:false
      
  },
  getlike(){
    let likelist = wx.getStorageSync('likelist');
    if(likelist){
        let index = likelist.findIndex(item=>{
            item == this.data.goodsList[0].id
        })
        console.log(index);
        if(index==-1){
            likelist.push(this.data.goodsList[0].id);
            wx.setStorageSync('likelist', likelist)
        }       
    }else{
        wx.setStorageSync('likelist', [this.data.goodsList[0].id])
    }
    this.setData({
        liked:true
    })
  },
  nolike(){
    let likelist = wx.getStorageSync('likelist');
        let index = likelist.findIndex(item=>{
            item == this.data.goodsList[0].id
        })   
            likelist.splice(index,1);
            wx.setStorageSync('likelist', likelist)
    this.setData({
        liked:false
    })
  },
  addCar(){
      wx.setStorageSync('good', "加入购物车数据");
      let arr = wx.getStorageSync('goodslist');
      if(arr){
          console.log(arr);
          var index = 0;
          for(let i = 0;i<arr.length;i++){
              index = arr[i].id == this.data.goodsList[0].id?i:-1;
          }
          // let index=arr.findIndex((item)=>{
          //     item.id==this.data.goodsList[0].id
          // });            
          console.log(index);
          if(index == -1){             
              arr.push(this.data.goodsList[0]);
              wx.setStorageSync('goodslist',arr);
          }else{
              return;
          }
          this.setData({
              carnum:wx.getStorageSync('goodslist').length
          })
      }else{
          wx.setStorageSync('goodslist',this.data.goodsList);
          this.setData({
            carnum:wx.getStorageSync('goodslist').length
        })
      }
  },
  goCart(){
      wx.switchTab({
        url: '/pages/car/car',
      })
  },
  goHome(){
      wx.switchTab({
        url: '/pages/home/home',
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
        getGoodsDetail({"goods_id":id}).then((data)=>{
            console.log(data);
            let {goods_id,goods_name,goods_small_logo,pics,goods_price,goods_introduce}  = data.message;
            let goodsList = [{
                id:goods_id,
                goodsImage:goods_small_logo,
                goodsName:goods_name,
                goodsImgs:pics,
                goodsAddress:"广州",
                goodsPrice:goods_price,
                goodsPriceOld:goods_price+100,
                goodsDetails:goods_introduce,
                num:1
            }]
            this.setData({
                goodsList
            })
        })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.setData({
          carnum:wx.getStorageSync('goodslist').length
      })
      console.log(this.data.carnum)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})